class_name FisheyeTransition
extends Control

@export var fisheye_strength_max: float = 20
@export var fisheye_duration: float = 0.5
@onready var color_rect: ColorRect = $ColorRect
@onready var mat: ShaderMaterial = null
@onready var bg: ColorRect = $Bg


func _ready() -> void:
	mat = color_rect.material as ShaderMaterial
	if mat == null:
		push_error("ShaderMaterial not found on ColorRect")


func _set_strength(value: float) -> void:
	mat.set_shader_parameter("strength", value)
	

func _set_fade(value: float) -> void:
	mat.set_shader_parameter("fade", value)


func fisheye_in(callback: Callable = Callable()):
	var start_value = mat.get_shader_parameter("strength")
	var tween = create_tween()
	tween.tween_method(_set_strength, start_value, fisheye_strength_max, fisheye_duration) \
		 .set_trans(Tween.TRANS_CUBIC).set_ease(Tween.EASE_IN_OUT)

	if callback.is_valid():
		tween.tween_callback(callback)
		
	var fade_start_value = mat.get_shader_parameter("fade")
	tween = create_tween()
	tween.tween_method(_set_fade, fade_start_value, 1.0, fisheye_duration) \
		 .set_trans(Tween.TRANS_CUBIC).set_ease(Tween.EASE_IN_OUT)


func fisheye_out(callback: Callable = Callable()):
	var start_value = mat.get_shader_parameter("strength")
	var tween = create_tween()
	tween.tween_method(_set_strength, start_value, 0.0, fisheye_duration) \
		 .set_trans(Tween.TRANS_CUBIC).set_ease(Tween.EASE_IN_OUT)
	
	if callback.is_valid():
		tween.tween_callback(callback)
	
	var fade_start_value = mat.get_shader_parameter("fade")
	tween = create_tween()
	tween.tween_method(_set_fade, fade_start_value, 0.0, fisheye_duration) \
		 .set_trans(Tween.TRANS_CUBIC).set_ease(Tween.EASE_IN_OUT)
		
		
